﻿Public Class dmNghiepVu1
    'state : 0 mặc định - trạng thái 1- tạo mới 2 sửa 3 - xóa
    Private state As Integer 'xử lý cho biết đang ở trạng thái nào
    'Load form
    Dim isLoad As Boolean = True 'Xét coi form đã load hay chưa
    Protected Overrides Function ProcessCmdKey(ByRef msg As System.Windows.Forms.Message, ByVal keyData As System.Windows.Forms.Keys) As Boolean
        If keyData = (Keys.Control Or Keys.N) Then
            Return getTaoMoi()
        End If
        If keyData = (Keys.Control Or Keys.S) Then
            Return getLuu()
        End If
        If keyData = (Keys.Control Or Keys.E) Then
            Return getSua()
        End If
        If keyData = (Keys.Control Or Keys.D) Then
            Return getXoa()
        End If
        If keyData = (Keys.Control Or Keys.P) Then
            'Return Me.getIn()
        End If
        If keyData = (Keys.Control Or Keys.R) Then
            Return Me.getLamTuoi
        End If
        If (keyData = (Keys.Control Or Keys.Q)) Then
            Return getThoat()
        End If
        Return MyBase.ProcessCmdKey(msg, keyData)
    End Function
    Private Sub setReadonly(ByVal isTrue As Boolean)
        Me.dmNghiepVuIUltraGrid_ThongTin.setReadonly(isTrue)
        Me.dmNghiepVu_chitiet_ThongTinChinh_IUltraGrid.setReadonly(isTrue)
        Me.dmNghiepVuIUltraGrid_DanhSach.setReadonly(isTrue)
        Me.dmNghiepVu_chitiet_ThongTinChinh_TimKiem_IUltraGrid.setReadonly(isTrue)
    End Sub
    Private Sub loadDuLieu()
        If isLoad Then
            Me.DmnghiepvuTableAdapter.Fill(Me.ErpharmaDataSet.dmnghiepvu)
            Me.Dmnghiepvu_chitietTableAdapter.Fill(Me.ErpharmaDataSet.dmnghiepvu_chitiet)
            Me.DmsochungtuTableAdapter.Fill(Me.ErpharmaDataSet.dmsochungtu)
            Me.DmtinhtrangTableAdapter.Fill(Me.ErpharmaDataSet.dmtinhtrang)
            Me.FormTableAdapter.Fill(Me.ErpharmaDataSet.form)
            Me.setReadonly(True)
            isLoad = False ' để load dữ liệu một lần
        End If
    End Sub
    Private Sub loadCauHinh() 'Set cấu hình cho tất cả các Ultragird, Ultracombo, Ultradropdown: set giao diện, set tìm kiếm, xử lý dữ liệu,groupby
        'set đối tượng xử lý dữ liệu
        Me.dmNghiepVuIUltraGrid_DanhSach.setDoiTuongXuLyDuLieu(Me.ErpharmaDataSet.dmnghiepvu, Me.DmnghiepvuBindingSource, Me.DmnghiepvuTableAdapter, Me.ErpharmaDataSet)
        Me.dmNghiepVuIUltraGrid_ThongTin.setDoiTuongXuLyDuLieu(Me.ErpharmaDataSet.dmnghiepvu, Me.DmnghiepvuBindingSource, Me.DmnghiepvuTableAdapter, Me.ErpharmaDataSet)
        Me.dmNghiepVu_chitiet_ThongTinChinh_IUltraGrid.setDoiTuongXuLyDuLieu(Me.ErpharmaDataSet.dmnghiepvu_chitiet, Me.Dmnghiepvu_chitietBindingSource1, Me.Dmnghiepvu_chitietTableAdapter, Me.ErpharmaDataSet)
        Me.dmNghiepVu_chitiet_ThongTinChinh_TimKiem_IUltraGrid.setDoiTuongXuLyDuLieu(Me.ErpharmaDataSet.dmnghiepvu_chitiet, Me.Dmnghiepvu_chitietBindingSource1, Me.Dmnghiepvu_chitietTableAdapter, Me.ErpharmaDataSet)
        Me.dmNghiepVuIUltraGrid_DanhSach.setThemIUltraDropDown(Me.DmsochungtuIUltraDropDown, Me.ErpharmaDataSet.dmsochungtu)
        Me.dmNghiepVuIUltraGrid_DanhSach.setThemIUltraDropDown(Me.DmtinhtrangIUltraDropDown, Me.ErpharmaDataSet.dmtinhtrang)
        Me.dmNghiepVuIUltraGrid_ThongTin.setThemIUltraDropDown(Me.DmsochungtuIUltraDropDown, Me.ErpharmaDataSet.dmsochungtu)
        Me.dmNghiepVuIUltraGrid_ThongTin.setThemIUltraDropDown(Me.DmtinhtrangIUltraDropDown, Me.ErpharmaDataSet.dmtinhtrang)
        Me.dmNghiepVu_chitiet_ThongTinChinh_IUltraGrid.setThemIUltraDropDown(Me.FormIUltraDropDown, Me.ErpharmaDataSet.form)
        'set group by mã để xử lý khi tìm kiếm chi tiết
        Me.dmNghiepVu_chitiet_ThongTinChinh_TimKiem_IUltraGrid.setGroupBy("ma")
        'set giao diện
        Me.dmNghiepVuIUltraGrid_DanhSach.setGiaoDienTuyBien()
        Me.dmNghiepVuIUltraGrid_ThongTin.setGiaoDien_Doi_ThongTin()
        Me.dmNghiepVu_chitiet_ThongTinChinh_IUltraGrid.setGiaoDienTuyBien()
        Me.dmNghiepVu_chitiet_ThongTinChinh_TimKiem_IUltraGrid.setGiaoDienVua()
        Me.DmsochungtuIUltraDropDown.setGiaoDien_An_ColumnHeader()
        Me.DmtinhtrangIUltraDropDown.setGiaoDien_An_ColumnHeader()
        Me.FormIUltraDropDown.setGiaoDien_An_ColumnHeader()
        Me.dmNghiepVuIUltraGrid_ThongTin.setMoveLast()
        'set tìm kiếm trên cột Ultragrid
        Me.dmNghiepVuIUltraGrid_DanhSach.setTimKiemTrenCotUltraGrid()
        Me.dmNghiepVu_chitiet_ThongTinChinh_TimKiem_IUltraGrid.setTimKiemTrenCotUltraGrid()
        Me.dmNghiepVuIUltraGrid_ThongTin.setFocusIUltraGrid("tinhtrang", Me.dmNghiepVu_chitiet_ThongTinChinh_IUltraGrid)
        'Set Menustrip cho ultragird
        Me.dmNghiepVu_chitiet_ThongTinChinh_IUltraGrid.setChoPhepMenuStrip(True, True, True, False, True, False, False, False)
        Me.setReadonly(True)
    End Sub
    Private Sub DmNghiepVu_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
       
        Me.Timer1.Start()
    End Sub
    'mã tự động của phiếu
    Private Function getMa() As String
        Try
            Dim ma As String = Me.HttientoTableAdapter.getMaKeToanTienMat(New Date(2012, 4, 1), New Date(2012, 4, 30))
            Return ma
        Catch ex As Exception
            Return Today.Year & Today.Month & Today.Day
        End Try
    End Function
    Private Function getTaoMoi() As Boolean
        If Me.dmNghiepVuIUltraGrid_ThongTin.getTaoMoi() Then
            Me.setReadonly(False)
            Me.state = 1
            Return True
        Else
            Return False

        End If
    End Function
    Private Sub btnTaoMoi_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTaoMoi.Click
        getTaoMoi()
    End Sub
    Private Function getLuu() As Boolean
        'Lưu cả form dmNghiepVu và dmNghiepVu_chitiet, Mục đích xuất hiện "Có lưu hay không"
        If Me.dmNghiepVuIUltraGrid_ThongTin.getCoThayDoiDuLieuKhong() Then ' Nếu có thay đổi dữ liệu trả về giá trị True
            If Me.dmNghiepVuIUltraGrid_ThongTin.getLuu() Then 'Lưu bảng cha
                Me.dmNghiepVu_chitiet_ThongTinChinh_IUltraGrid.getLuuNgam() 'Lưu bảng con
                Me.state = 0 ' sau khi lưu xong trả về trạng thái mặc định
                Me.setReadonly(True)
                Return True
                Exit Function
            End If
        ElseIf Me.dmNghiepVu_chitiet_ThongTinChinh_IUltraGrid.getCoThayDoiDuLieuKhong() Then
            If Me.dmNghiepVu_chitiet_ThongTinChinh_IUltraGrid.getLuu() Then 'lưu bảng con
                Me.state = 0
                Me.setReadonly(True)
                Return True
                Exit Function
            End If
        End If
        Return False
    End Function

    Private Sub btnLuu_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLuu.Click
        getLuu()
    End Sub
    Private Function getXoa() As Boolean
        If Me.dmNghiepVuIUltraGrid_ThongTin.getXoa() Then
            Me.state = 0
            Return True
        Else
            Return False
        End If
    End Function
    Private Sub btnXoa_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnXoa.Click
        getXoa()
    End Sub
    Private Function getLamTuoi() As Boolean
        Try
            Dim position As Integer = Me.DmnghiepvuBindingSource.Position 'Lưu lại vị trí con tró trên dmNghiepVu để gán đúng vị trí khi load lại
            Me.isLoad = True 'gán bằng true để load lại
            Me.state = 0 'trả về trạng thái 0
            Me.DmnghiepvuBindingSource.Position = position 'vị trí hiện tại
            Return True
        Catch ex As Exception
            Return False
        End Try
    End Function
    Private Sub btnLamTuoi_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLamTuoi.Click
        getLamTuoi()
    End Sub

    Private Sub btnIn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnIn.Click
        'MessageBox.Show(Me.Ketoantienmatchitiet_ThongTinKhaiThue_IUltraGrid.Rows(2).ChildBands(0).Rows.SummaryValues("doanhthu").Value)
    End Sub
    Private Function getThoat() As Boolean
        If Me.getLuu() Then
            Me.Dispose()
            Me.Close()
            Return True
        Else
            Me.Dispose()
            Me.Close()
            Return False
        End If
    End Function
    Private Sub btnThoat_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnThoat.Click
        getThoat()
    End Sub
    Private Function getSua() As Boolean
        Try
            Me.setReadonly(False)
            Me.state = 2
        Catch ex As Exception
            Return False
        End Try
        Return True
    End Function
    Private Sub btnSua_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSua.Click
        getSua()
    End Sub
    'Chạy processbar phụ thuộc vào đồng hồ
    Private Sub Timer1_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        Me.ultraProgressBar1.IncrementValue(10)
        If Me.ultraProgressBar1.Value = Me.ultraProgressBar1.Maximum / 2 Then
            loadDuLieu()
        End If
        If Me.ultraProgressBar1.Value = Me.ultraProgressBar1.Maximum Then
            Me.loadCauHinh()
            Me.state = 0
            Me.Timer1.Stop()
            Me.ultraProgressBar1.Hide()
        End If
    End Sub
    'Xử lý click phần tìm kiếm chi tiết để quay lại các phiếu cha

    Private Sub DmNghiepVu_chitiet_ThongTinChinh_TimKiem_IUltraGrid_ClickCell(ByVal sender As Object, ByVal e As Infragistics.Win.UltraWinGrid.ClickCellEventArgs)
        If Me.dmNghiepVu_chitiet_ThongTinChinh_TimKiem_IUltraGrid.ActiveRow.Index <> 1 Then
            Try
                Me.DmnghiepvuBindingSource.Position = Me.DmnghiepvuBindingSource.Find("ma", Me.dmNghiepVu_chitiet_ThongTinChinh_TimKiem_IUltraGrid.ActiveRow.Cells("ma").Value)
            Catch ex As Exception

            End Try
        End If
    End Sub
End Class